package view.controller;

import common.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Callback;
import server.ClientConnServer;
import tools.GetTime;
import view.*;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class GroupchatController implements Initializable {
    public ScrollPane groupscrollpane;
    public FlowPane groupmsglist;
    public ImageView file;
    public ImageView emo;
    public ImageView photo;
    public TextArea talktext;
    public TextArea groupintroduction;
    public Label groupname;
    public Label number;
    public Label groupid;
    public Label groupimg;
    public ListView<GroupDataing> grmemberlist;
    public Button sendbt;
    public boolean last = true;
    public File localFile;
    public ObservableList<GroupDataing> groupmenmber = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        groupname.setText(NowUser.group.getGroupname());
        System.out.println(groupname.getText());
        groupid.setText(NowUser.group.getGroupid());
        groupintroduction.setText(NowUser.group.getGroupsign());
        groupintroduction.setEditable(false);
        groupintroduction.setWrapText(true);
        if(NowUser.group.getGroupheadimage()==null || NowUser.group.getGroupheadimage().equals("") || NowUser.group.getGroupheadimage().equals("null")){
            groupimg.setText("E:\\untitled\\src\\view\\photo\\群头像.jpg");
        }else {
            System.out.println(NowUser.group.getGroupheadimage());
            groupimg.setText(NowUser.group.getGroupheadimage());
        }
        Message newmsg=new Message();
        newmsg.setType("grouprecords");//聊天记录
        Communication comm=new Communication();
        Group group=new Group();
        group.setGroupid(groupid.getText());
        comm.setGroup(group);
        newmsg.setCommunication(comm);
        try {
            new ClientConnServer().sendinfo(newmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            ArrayList<Communication> groupchatlist=NowUser.groupchatlist;
            if(groupchatlist!=null){
                for(int i=0;i<groupchatlist.size();i++)
                {
                    Communication communication=new Communication();
                    communication=groupchatlist.get(i);
                    System.out.println(communication.getSender().getId());
                    if(communication.getSender().getId().equals(NowUser.user.getId())){
                        addMessageBoxr(communication);
                    }else{
                        addMessageBoxl(communication);
                    }
                }}
            ManageChat.setGroupScrollPaneHashMap(NowUser.user.getId()+groupid.getText(),groupscrollpane);
            ManageChat.setGroupMeesagelist(NowUser.user.getId()+groupid.getText(),groupmsglist);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        //设置元素的全高
        VBox.setVgrow(groupmsglist, Priority.ALWAYS);
        //回车事件，返回文本框内容
        talktext.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                if (keyEvent.getCode() == KeyCode.ENTER) {
                    Message msg = new Message();
                    Communication comm = new Communication();
                    comm.setContent(talktext.getText());
                    comm.setSender(NowUser.user);
                    String time=new GetTime().getTime();//获取时间的工具类
                    comm.setSendDateTime(time);
                    String groupid1=groupid.getText();
                    Group gp=new Group();
                    gp.setuserid(groupid1);
                    comm.setGroup(gp);
                    comm.setType(0);
                    msg.setType("groupchatsend");
                    msg.setCommunication(comm);
                    NowUser.chatsend = false;
                    // System.out.println("ggggggg");
                    try {
                        new ClientConnServer().sendinfo(msg);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    addMessageBoxr(comm);
                    talktext.setText("");
                }
            }
        });

        //刚打开页面聊天内容显示在最底部
        groupscrollpane.setVvalue(1);
        groupscrollpane.getVvalue();
        //让发送消息显示在最底部
        groupscrollpane.vvalueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                //判断滑轮是否在最下面
                if (last) {
                    groupscrollpane.setVvalue(1.0);
                    last = false;
                }
            }
        });

        //群成员初始化
        Message membermsg = new Message();
        Group gp = new Group();
        gp.setGroupid(groupid.getText());
        membermsg.setGroup(gp);
        membermsg.setType("getmembers");
        ArrayList<Group> memberstatement = new ArrayList<Group>();
        ArrayList<User> listm = new ArrayList<User>();
        try {
            new ClientConnServer().sendinfo(membermsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            memberstatement =NowUser.getGroupmemberstatement();
            listm = NowUser.getGroupmembers();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        grmemberlist.getSelectionModel().select(0);

        if(listm!=null) {
            for (int i = 0; i < listm.size(); i++) {

                if (listm.get(i).getHeadimage() == null || listm.get(i).getHeadimage().equals("") || listm.get(i).getHeadimage().equals("null")) {
                    User uu = listm.get(i);
                    uu.setHeadimage("E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                    listm.set(i, uu);
                }
                Image img = new Image("file:" +listm.get(i).getHeadimage());
                groupmenmber.add(new GroupDataing(listm.get(i).getName(), listm.get(i).getId(), img, listm.get(i).getHeadimage()));
            }
        }
        grmemberlist.setItems(groupmenmber);

        //获取好友列表每个单元的值
        grmemberlist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<GroupDataing>() {
            @Override
            public void changed(ObservableValue<? extends GroupDataing> observable, GroupDataing oldValue, GroupDataing newValue) {

            }
        });

        //好友列表的点击事件
        grmemberlist.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开聊天室
                if(event.getClickCount()==2 && event.getButton().equals(MouseButton.PRIMARY)){
                    Stage stage = (Stage) grmemberlist.getScene().getWindow();
                    User user = new User();
                    user.setId(grmemberlist.getFocusModel().getFocusedItem().getSingleid());
                    Message tmpmsg=new Message();
                    tmpmsg.setUser(user);
                    tmpmsg.setType("getfriend");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setFriend(user);
                    Chat rj=new Chat();
                    FXMLLoader fxmlLoader=new FXMLLoader();
                    fxmlLoader.setLocation(getClass().getResource("../fxml/chat.fxml"));
                    try {
                        fxmlLoader.load();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ChatController chattingController=fxmlLoader.getController();
                    //System.out.println(Common.user.getUserid()+Common.friend.getUserid()+"sss");
                    ManageChat.setChatcontroller(NowUser.user.getId()+NowUser.friend.getId(),chattingController);
                    ManageChat.setChat(NowUser.user.getId()+NowUser.friend.getId(),rj);

                    System.out.println(ManageChat.getchatcontroller(NowUser.user.getId()+NowUser.friend.getId()));
                    // System.out.println(Common.user.getUserid()+Common.friend.getUserid());
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }//右键删除群成员
                if(event.getClickCount()==1 && event.getButton().equals(MouseButton.SECONDARY)){
                    Stage stage = (Stage) grmemberlist.getScene().getWindow();
                    Stage newstage = new Stage();
                    //System.out.println("sb"+friendlistview.getFocusModel().getFocusedItem().getSingleid());
                    User friendu=new User();
                    friendu.setId(grmemberlist.getFocusModel().getFocusedItem().getSingleid());
                    Message tmpmsg=new Message();
                    tmpmsg.setUser(friendu);
                    tmpmsg.setType("deletemember");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setFriend(friendu);
                    DeleteFriend rj=new DeleteFriend();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //将listView设置为单元工厂
        grmemberlist.setCellFactory(new Callback<ListView<GroupDataing>, ListCell<GroupDataing>>() {
            @Override
            public ListCell<GroupDataing> call(ListView<GroupDataing> param) {
                return new ListCell<GroupDataing>(){
                    @Override
                    protected void updateItem(GroupDataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if(!empty){
                            setStyle("-fx-background-color:transparent");
                            HBox hBox=new HBox();
                            //导入好友列表图像
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/groupsingle.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("群成员加载错误");
                            }
                            GroupsingleController groupsingleController=fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            groupsingleController.setGname(item.singlename);
                            groupsingleController.setGmemberimg(item.singleimage);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }

    public void tosend(ActionEvent actionEvent) throws IOException, BrokenBarrierException, InterruptedException {
        Message msg = new Message();
        Communication comm = new Communication();
        comm.setContent(talktext.getText());
        comm.setSender(NowUser.user);
        String time=new GetTime().getTime();//获取时间的工具类
        comm.setSendDateTime(time);
        String groupid1=groupid.getText();
        //System.out.println(groupid+"ggg");
        Group group=new Group();
        group.setGroupid(groupid1);
        comm.setGroup(group);
        comm.setType(0);
        msg.setType("groupchatsend");
        msg.setCommunication(comm);
        NowUser.chatsend = false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        System.out.println("iiiiiii");
        addMessageBoxr(comm);
        //addMessageBoxl(comm);
        talktext.setText("");
    }

    public void groupinformation(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException{
        Message msg = new Message();
        Group gp = new Group();
        gp.setGroupid(groupid.getText());
        gp.setuserid(NowUser.user.getId());
        msg.setGroup(gp);
        msg.setType("groupinformation");
        NowUser.groupowner=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        System.out.println(NowUser.groupowner);
        if(!NowUser.groupowner){
            Stage stage = (Stage) groupname.getScene().getWindow();
            GroupInfo rj = new GroupInfo();
            Stage newstage = new Stage();
            try {
                rj.start(newstage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            Stage stage = (Stage) groupname.getScene().getWindow();
            GroupInfoOwner rj = new GroupInfoOwner();
            Stage newstage = new Stage();
            try {
                rj.start(newstage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //右气泡
    public void addMessageBoxr(Communication communication) {
        // System.out.println(chatfriendid.getText());
        URL url = null;
        try {
            if(communication.getSender().getHeadimage()==null || communication.getSender().getHeadimage().equals("") || communication.getSender().getHeadimage().equals("null")){
                url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
            }else {
                url = new URL("file:" + communication.getSender().getHeadimage());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Image image = new Image(String.valueOf(url));
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Circle circle = new Circle();
        circle.setCenterX(25);
        circle.setCenterY(25);
        circle.setRadius(25);
        imageView.setClip(circle);
        Label messageBubble=new Label();
        //System.out.println("sssssss");
        //System.out.println(communication.getType());
        if(communication.getType()==0) {
            messageBubble = new Label(communication.getContent());
            //换行
            messageBubble.setWrapText(true);
            messageBubble.setMinWidth(25);
            messageBubble.setMaxWidth(220);
            messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            messageBubble.setPadding(new Insets(6));
            messageBubble.setFont(new Font(14));
            HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(505);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(messageBubble, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = groupscrollpane.getVvalue() == 1.0;
            //System.out.println(messagesList);
            //messagesList=this.messagesList;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==1){//发送图片
            Image imgpath = new Image("file:"+communication.getContent());
            ImageView imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            //System.out.println(communication.getSendDateTime()+"可以");
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(505);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = groupscrollpane.getVvalue() == 1.0;
            //System.out.println(messagesList);
            //messagesList=this.messagesList;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==2){//发送文件
            Image imgpath = new Image("file:"+"E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
            ImageView imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(100);
            imgv.setFitWidth(100);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    File opfile=new File(communication.getContent());
                    if(opfile.exists()){
                        Desktop desktop = Desktop.getDesktop();
                        try {
                            desktop.open(opfile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            //System.out.println(communication.getSendDateTime()+"可以");
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(514);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = groupscrollpane.getVvalue() == 1.0;
            //System.out.println(messagesList);
            //messagesList=this.messagesList;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==4){//发送表情
            Image imgpath = new Image("file:"+communication.getContent());
            ImageView imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(35);
            imgv.setFitWidth(35);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            //System.out.println(communication.getSendDateTime()+"可以");
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(505);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = groupscrollpane.getVvalue() == 1.0;
            //System.out.println(messagesList);
            //messagesList=this.messagesList;
            groupmsglist.getChildren().add(messageBox);
        }
    }

    //左气泡
    public void addMessageBoxl(Communication communication) {
        System.out.println(groupimg.getText());
        URL url = null;
        try {
            if(communication.getSender().getHeadimage()==null || communication.getSender().getHeadimage().equals("") || communication.getSender().getHeadimage().equals("null")){
                url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
            }else {
                url = new URL("file:" + communication.getSender().getHeadimage());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Image image = new Image(String.valueOf(url));
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Circle circle = new Circle();
        circle.setCenterX(25);
        circle.setCenterY(25);
        circle.setRadius(25);
        imageView.setClip(circle);
        if(communication.getType()==0) {
            Label messageBubble = new Label(communication.getContent());
            messageBubble.setWrapText(true);
            messageBubble.setMaxWidth(220);
            messageBubble.setMinWidth(25);
            messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            messageBubble.setPadding(new Insets(6));
            messageBubble.setFont(new Font(14));
            HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, messageBubble);
            last = groupscrollpane.getVvalue() == 1.0;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==1) {//发送图片
            Image imgpath = new Image("file:" + communication.getContent());
            ImageView imgv = new ImageView(imgpath);
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = groupscrollpane.getVvalue() == 1.0;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==2){//发送文件
            Image imgpath = new Image("file:"+"E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
            ImageView imgv = new ImageView(imgpath);
            //System.out.println(communication.getContent()+"怕怕怕怕怕");
            //换行
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    File opfile=new File(communication.getContent());
                    if(opfile.exists()){
                        Desktop desktop = Desktop.getDesktop();
                        try {
                            desktop.open(opfile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = groupscrollpane.getVvalue() == 1.0;
            groupmsglist.getChildren().add(messageBox);
        }else if(communication.getType()==4) {//发送表情
            Image imgpath = new Image("file:" + communication.getContent());
            ImageView imgv = new ImageView(imgpath);
            imgv.setFitHeight(35);
            imgv.setFitWidth(35);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = groupscrollpane.getVvalue() == 1.0;
            groupmsglist.getChildren().add(messageBox);
        }
    }

    public void tofile(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException{
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            //读取本地文件
            localFile = new File(str);
            //emoji.setImage(new Image(url,127,108,true,true,true));
        }
        String groupid1=groupid.getText();
        Message msg = new Message();
        Group group=new Group();
        Communication comm = new Communication();
        group.setGroupid(groupid1);
        comm.setGroup(group);
        comm.setSendDateTime(new GetTime().getTime());
        comm.setSender(NowUser.user);
        comm.setType(2);
        msg.setHeadimage(localFile);
        msg.setType("sendgroupfile");
        msg.setCommunication(comm);
        NowUser.sendphoto = false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        addMessageBoxr(NowUser.communication);
        talktext.setText("");
    }

    public void toemoji(MouseEvent mouseEvent) {
        Group group = new Group();
        group.setGroupid(groupid.getText());
        NowUser.group = group;
        Stage stage = (Stage) emo.getScene().getWindow();
        Stage newstage=new Stage();
        GroupEmoji rj=new GroupEmoji();
        try {
            rj.start(newstage);
//                        rj.Show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void tophoto(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException {
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            //读取本地文件
            localFile = new File(str);
            //emoji.setImage(new Image(url,127,108,true,true,true));
        }
        String groupid1=groupid.getText();
        Message msg = new Message();
        Group group=new Group();
        Communication comm = new Communication();
        group.setGroupid(groupid1);
        comm.setGroup(group);
        comm.setSendDateTime(new GetTime().getTime());
        comm.setSender(NowUser.user);
        comm.setType(1);
        msg.setHeadimage(localFile);
        msg.setType("sendgroupphoto");
        msg.setCommunication(comm);
        System.out.println(msg.getCommunication().getGroup().getGroupid()+"sbabab");
        NowUser.sendphoto = false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        addMessageBoxr(NowUser.communication);
        talktext.setText("");
    }
}

class GroupDataing  {
    String singleimagepath;
    String singlename;
    String singleqm;
    String singleid;
    Image singleimage;

    public String getSingleimagepath() {
        return singleimagepath;
    }
    public void setSingleimagepath(String singleimagepath) {
        this.singleimagepath = singleimagepath;
    }

    public String getSingleid() {
        return singleid;
    }
    public void setSingleid(String singleid) {
        this.singleid = singleid;
    }

    public Image getSingleimage() {
        return singleimage;
    }
    public void setSingleimage(Image singleimage) {
        this.singleimage = singleimage;
    }

    public String getSinglename() {
        return singlename;
    }
    public void setSinglename(String singlename) {
        this.singlename = singlename;
    }

    public String getSingleqm() {
        return singleqm;
    }
    public void setSingleqm(String singleqm) {
        this.singleqm = singleqm;
    }

    public GroupDataing(String name, String id, Image imgv, String imgvpath) {
        this.singlename = name;
        this.singleid = id;
        this.singleimage = imgv;
        this.singleimagepath = imgvpath;
    }
}